package de.aktiencrawler;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import de.preisrobot.entity.Kurs;
import de.preisrobot.persistence.dao.AktienKurseDao;
import de.preisrobot.service.helper.HttpClientFactory;
import de.scheduler.Timer;

public class Finanzen_netParser {

    private static Logger log = LoggerFactory.getLogger(Finanzen_netParser.class);

    public Kurs parse(final String sourceUrlString) {
        final HttpClient client = HttpClientFactory.getHttpClient();
        final GetMethod method = HttpClientFactory.createGETMethod(sourceUrlString);

        try {
            client.executeMethod(method);
            final InputStream in = method.getResponseBodyAsStream();
            final Kurs k = parseStream(in);
            return k;

        }

        catch (final Exception e) {

            log.error("", e);

        } finally {

            method.releaseConnection();
        }
        return null;
    }

    private Kurs parseStream(final InputStream in) throws IOException {
        final Kurs kurs = new Kurs();
        kurs.setKursZeit(new Date());

        final Source s = new Source(in);

        final List<Element> allElementsByClass = s.getAllElementsByClass("inhalt t5");
        final List<Element> allElementsTiel = s.getAllElementsByClass("titel");

        if (allElementsByClass.isEmpty())
            return null;

        parsePreis(kurs, allElementsByClass);
        parseTitel(kurs, allElementsTiel);

        System.out.println(kurs);

        return kurs;
    }

    private void parseTitel(final Kurs k, final List<Element> allElementsTiel) {

        if (!allElementsTiel.isEmpty()) {
            final String string = allElementsTiel.get(0).getTextExtractor().toString();

            k.setTitel(string);
        }

    }

    private void parsePreis(final Kurs k, final List<Element> allElementsByClass) {
        final List<Element> allElementsByClass2 = allElementsByClass.get(0).getAllElementsByClass("noBL");
        final Element element = allElementsByClass2.get(0);
        final String string = element.getTextExtractor().toString();
        final String[] split = string.split(" ");

        k.setPreis(split[0]);
    }

    public static void main(final String[] args) throws InterruptedException {

        final ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
        final AktienKurseDao dao = (AktienKurseDao) appContext.getBean("aktienkurseDao");

        final Finanzen_netParser f = new Finanzen_netParser();

        final Timer t = new Timer(Timer.EINE_MINUTE);
        t.setStartTime(9, 00, 00);
        t.setStopTime(19, 00, 00);

        Kurs kurs = null;

        while (true) {
            try {

                kurs = f.parse("http://www.finanzen.net/aktien/Google-Aktie");
                dao.save(kurs);

                kurs = f.parse(" http://www.finanzen.net/aktien/MAN-Aktie");
                dao.save(kurs);

                kurs = f.parse("http://www.finanzen.net/aktien/Commerzbank-Aktie");
                dao.save(kurs);

                final long sleep = t.getSleepTime();
                System.out.println(sleep);
                Thread.sleep(45000);

            } catch (final Exception e) {
                System.err.println(kurs);
                e.printStackTrace();
            }
        }
    }
}
